home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 613 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.6 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.std.c
  4. Subject: Re: Paradox due to A7.1 Pointer generation rule
  5. Date: Wed, 20 Mar 96 22:32:04 GMT
  6. Organization: none
  7. Message-ID: <827361124snz@genesis.demon.co.uk>
  8. References: <4ikp70$mkf@franklin.its.utas.edu.au>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4ikp70$mkf@franklin.its.utas.edu.au>
  15.            vmm@tasman.its.utas.edu.au "Vishv Malhotra" writes:
  16.  
  17. >
  18. >In the following function a++; is acceptable but b++; is not.
  19. >
  20. >        void foo(int a[20]) {
  21. >                int b[20];
  22. >
  23. >                a++;
  24. >                b++;
  25. >        }
  26. >
  27. >I understand the reason, but I wish to know if there is any thought
  28. >on elimination the anomaly?
  29.  
  30. That would gratuitously break existing code for no practical gain.
  31.  
  32. You refer in the subject line to A7.1 which in K&R describes how in an
  33. expression an array evaluates to a non-lvalue pointer to its first element.
  34. That explains why b++ is illegal (since ++ requires its operand to be an
  35. lvalue) but is not relevant to a. That is because a declaration is not an
  36. expression and a different rule (commonly called the rewrite rule) applies
  37. to function parameters. In essence a parameter of type array of T is
  38. rewritten to have type pointer to T. So the definition of foo() above means
  39. precisely the same thing as one defined as:
  40.  
  41.          void foo(int *a) {
  42.            ...
  43.          }
  44.  
  45. Clearly this mirrors the rule for array evaluation (and there is a similar
  46. case for functions/function pointers) but the rewrite rule affects the
  47. fundamental type of an object rather than just the value derived from it.
  48. In a++ in the code above ++ is genuinely being applied to a pointer object
  49. and, for example, sizeof a == sizeof(int *). a really is a pointer whereas
  50. b is not.
  51.  
  52. I suppose you could eliminate the rewrite rule for arrays but I don't
  53. think that would be helpful. It would also generate as many anomalies as
  54. it eliminates.
  55.  
  56. >Perhaps, I should state my problem more clearly, in K&R on page 115
  57. >where command line arguments are being explained the diagram for argv
  58. >corresponds to the declaration
  59. >
  60. >        char *(*argv)[]
  61.  
  62. No, it corresponds to:
  63.  
  64.          char **
  65.  
  66. 'Pointer to an array' tends to be used rather loosely to mean 'pointer to
  67. an element (usually the first) of an array'. The point is that once you
  68. can address one element of an array you can address all of the others using
  69. pointer arithmetic so, for instance when you have:
  70.  
  71.     int a[10];
  72.     int *i = a;
  73.  
  74. you can use i to address any element of a. Some (including K&R seemingly)
  75. say that i points to array a. IMHO this is a poor description but it is a
  76. usage you have to be aware of.
  77.  
  78. >I have never seen such a declaration! Nor does this seem to work on
  79. >my compiler. Is the diagram on page 115 not true representation of
  80. >ANSI C standard?
  81.  
  82. I think the arrow from argv should point inside the the box that represents
  83. the array to the first element, not just at the box itself. K&R is really
  84. just following diagramatically the loose usage of terminology.
  85.  
  86. Note that unless your question is directly relevant to the contents of
  87. the ANSI/ISO C standard document rather than the C language in general you
  88. should post it to comp.lang.c or comp.lang.c.moderated. K&R no longer
  89. holds the position of language standard (not even the appendices).
  90.  
  91. -- 
  92. -----------------------------------------
  93. Lawrence Kirby | fred@genesis.demon.co.uk
  94. Wilts, England | 70734.126@compuserve.com
  95. -----------------------------------------
  96.